package community.hospital.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import community.hospital.annotation.Log;
import community.hospital.annotation.Module;
import community.hospital.annotation.NeedLogin;
import community.hospital.annotation.NeedPermission;
import community.hospital.common.*;
import community.hospital.entity.Hospital;
import community.hospital.entity.request.HospitalRequest;
import community.hospital.entity.request.ListHospitalRequest;
import community.hospital.entity.response.HospitalOptionResp;
import community.hospital.entity.response.HospitalResponse;
import community.hospital.service.application.HospitalApplicationService;
import org.checkerframework.checker.lock.qual.LockHeld;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.List;

import static community.hospital.common.Constant.*;
import static community.hospital.common.PermissionConstant.*;

/**
 * @Author：ydj
 * @Date：2023/1/27 14:57
 */
@RestController
@RequestMapping("/hospital")
@Module("医院")
public class HospitalController {

    @Resource
    private HospitalApplicationService hospitalApplicationService;

    @Log(methodName = LOG_QUERY,desc = "分页查询医院")
    @PostMapping("/listHospital")
//    @NeedLogin
//    @NeedPermission(role = SUPER_ADMIN, permission = LIST_HOSPITAL)
    public R<Page<Hospital>> listHospital(@Valid @RequestBody ListHospitalRequest request) {
        Page<Hospital> responsePage = hospitalApplicationService.listHospital(request);
        return R.ok(responsePage);
    }
    @Log(methodName = LOG_QUERY,desc = "查询医院信息")
    @GetMapping("/query")
    public R<HospitalResponse> getHospital(Long id) {
        HospitalResponse response = hospitalApplicationService.getHospital(id);
        return R.ok(response);
    }

    @Log(methodName = LOG_INSERT,desc = "新增医院")
    @PostMapping("/addHospital")
    @NeedLogin
    @NeedPermission(role = SUPER_ADMIN, permission = ADD_HOSPITAL)
    public R<Boolean> addHospital(@Validated(Add.class) @RequestBody HospitalRequest request) {
        Boolean isOk = hospitalApplicationService.addHospital(request);
        return R.ok(isOk);
    }
    @Log(methodName = LOG_UPDATE,desc = "更新医院")
    @PostMapping("/updateHospital")
    @NeedLogin
    @NeedPermission(role = SUPER_ADMIN, permission = UPDATE_HOSPITAL)
    public R<Boolean> updateHospital(@Validated(Update.class) @RequestBody HospitalRequest request) {
        Boolean isOk = hospitalApplicationService.updateHospital(request);
        return R.ok(isOk);
    }
    @Log(methodName = LOG_REMOVE,desc = "删除医院")
    @PostMapping("/removeHospital")
    @NeedLogin
    @NeedPermission(role = SUPER_ADMIN, permission = REMOVE_HOSPITAL)
    public R<Boolean> updateHospital(@Valid @RequestBody BaseRemove baseRemove) {
        Boolean isOk = hospitalApplicationService.removeHospital(baseRemove.getIds());
        return R.ok(isOk);
    }
    @PostMapping("/isHospitalExist")
    public R<Boolean> isHospitalExit(@RequestBody Hospital hospital){
        Boolean isOk =  hospitalApplicationService.isHospitalExist(hospital);
        return R.ok(isOk);
    }

    /**
     * 查询所有医院可选项
     * @return
     */
    @GetMapping("/listOptionHospital")
    public R<List<HospitalResponse>> listOptionHospital() {
        List<HospitalResponse> responseList = hospitalApplicationService.listOptionHospital();
        return R.ok(responseList);
    }




}
